package top.infopub.mgr.fia.service.impl;


import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.fia.dao.ArPlanDao;
import top.infopub.mgr.fia.dao.ArPlanDetailDao;
import top.infopub.mgr.fia.domain.ArPlanDomain;
import top.infopub.mgr.fia.service.ArPlanService;
import top.infopub.mgr.fia.vo.ArPlanVo;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.model.fia.ArPlan;
import top.infopub.model.fia.ArPlanDetail;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class ArPlanServiceImpl extends GenericServiceImpl<ArPlan, String> implements ArPlanService {

    @Autowired
    private ArPlanDao arPlanDao;

    @Autowired
    private ArPlanDetailDao arPlanDetailDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<ArPlan, String> getDao() {
        return arPlanDao;
    }

    @Override
    public Page<ArPlanDomain> selectPageByDomainWhere(Page<ArPlanDomain> page, ArPlanVo arPlanVo) {
        // 权限内数据
        arPlanVo.setVisibleProCodes(wsFlowProcess.proxyVisibleProCodes());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ArPlanDomain>)arPlanDao.selectByDomainWhere(arPlanVo);
    }

    @Override
    public ArPlanVo selectById(String id) {
        return arPlanDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addArPlan(ArPlanVo arPlan, List<ArPlanDetail> arPlanDetails) {
        //插入预立项记录
        arPlanDao.insertById(arPlan);

        //批量插入子项
        if (CollectionUtils.isNotEmpty(arPlanDetails)) {
            arPlanDetailDao.insertByBatch(arPlanDetails);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void udpArPlan(ArPlanVo arPlanVo) {
        
        ArPlan arPlan = arPlanDao.selectByPrimaryKey(arPlanVo.getId());
        BeanUtils.copyProperties(arPlanVo, arPlan);
        arPlan.setProcessStatus("9");//状态改为已生效
        arPlan.setUptTime(new Date());
        arPlan.setUptBy(SessionUserUtils.getSessionUserId());
        this.updateBySelective(arPlan);

        //批量更新付款明细
        arPlanDetailDao.updateByBatch(arPlanVo.getPlanDetails());

    }

    @Override
    public int count(Integer belongsYear,String proCode) {
        return arPlanDao.count(belongsYear,proCode);
    }

}